package com.bjshiwei.zhengu.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bjshiwei.zhengu.R;
import com.bjshiwei.zhengu.bean.UpAndDownBean;

import java.util.List;

/**
 * 作者：Administrator on 2016/10/10 0010 16:33
 * 邮箱：sanjins@126.com
 */


public class UpAndDownAdapter extends MyBaseAdapter<UpAndDownBean> {
    private List<UpAndDownBean> mData;
    private Context mContext;
    public UpAndDownAdapter(Context context, List<UpAndDownBean> datas) {
        super(context,datas);
        mData =datas;
        mContext =context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if(convertView==null) {
            convertView = View.inflate(mContext, R.layout.item_updown,null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if(!TextUtils.isEmpty(mData.get(position).getName())) {
            viewHolder.mTextViewName.setText(mData.get(position).getName());
        }

        if(!TextUtils.isEmpty(mData.get(position).getId())) {
            viewHolder.mTextViewId.setText(mData.get(position).getId());
        }

        if(!TextUtils.isEmpty(mData.get(position).getPrice())) {
            viewHolder.mTextViewPrice.setText(mData.get(position).getPrice());
        }

        if(!TextUtils.isEmpty(mData.get(position).getChangeRate())) {

            if(!mData.get(position).getChangeRate().contains("-")) {
                viewHolder.mTextViewChangeRate.setTextColor(mContext.getResources().getColor(R.color.stockRed));
                viewHolder.mTextViewChangeRate.setText("+"+mData.get(position).getChangeRate()+"%");
            }else{
                viewHolder.mTextViewChangeRate.setTextColor(mContext.getResources().getColor(R.color.green));
                viewHolder.mTextViewChangeRate.setText(mData.get(position).getChangeRate()+"%");
            }
        }

        return convertView;
    }

   static class ViewHolder{
        private TextView mTextViewName ,mTextViewId,mTextViewPrice,mTextViewChangeRate;
        public ViewHolder(View view){
            mTextViewName = (TextView) view.findViewById(R.id.tv_updown_name);
            mTextViewId = (TextView) view.findViewById(R.id.tv_updown_id);
            mTextViewPrice = (TextView) view.findViewById(R.id.tv_updown_price);
            mTextViewChangeRate = (TextView) view.findViewById(R.id.tv_updown_changerate);
        }
    }
}
